package cn.cocho.android.mct.comp.tab;

import android.view.Display;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import cn.cocho.android.mct.R;
import cn.cocho.android.mc.activity.BaseActivity;

public class TabTest extends BaseActivity implements OnCheckedChangeListener{

	private int prePos=-1;
	private ImageView ivMove;
	private int ivMoveWidth=0;
	private int tabCount=3;

	@Override
	public void init(){
		setContentView(R.layout.tab_test);
		
		RadioGroup mainTab=(RadioGroup)findViewById(R.id.main_tab);
        mainTab.setOnCheckedChangeListener(this);
		
		Display display=this.getWindowManager().getDefaultDisplay();
		ivMoveWidth=display.getWidth()/tabCount;
    	ivMove=(ImageView)findViewById(R.id.iv_move);
    	ivMove.setLayoutParams(new FrameLayout.LayoutParams(ivMoveWidth,LayoutParams.FILL_PARENT));
    
    	setAnimation(R.id.tab1,0);
    	
	}
	
	public void createAnimation(final int currentPos,int previousPos){
		if(previousPos==-1){
			previousPos=0;
		}
		
		int start=previousPos*ivMoveWidth;
		final int end=currentPos*ivMoveWidth;
		Animation animation = new TranslateAnimation(start, end,0, 0); 
		animation.setDuration(500);
		animation.setFillAfter(true);//动画结束后停在结束位置。但如果添加点击事件，点击位置在原来的位置。
		ivMove.startAnimation(animation);
	}

	private void setAnimation(int currentRbId,int currentPos){
		createAnimation(currentPos,prePos);
		prePos=currentPos;
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// TODO Auto-generated method stub
		switch (checkedId) {
		case R.id.tab1:
			setAnimation(checkedId, 0);
			break;
		case R.id.tab2:
			setAnimation(checkedId, 1);
			break;
		case R.id.tab3:
			setAnimation(checkedId, 2);
			break;
		}

	}
}
